<?php

if(! defined('OT_ROOT')) {
	exit('Access Denied');
}


class AppMoneyRecord{

	public static function Jud(){
		return true;
	}

	public static function Show($str, $def=''){
		if (self::Jud()){
			return $str;
		}else{
			return $def;
		}
	}


	// 后台 会员管理
	public static function UsersItem($userID, $color='red'){
		return '<div style="padding-top:2px;color:'. $color .';cursor:pointer;" onclick=\'OT_OpenUserMoney("&userID='. $userID .'")\'>[财务管理]</div>';
	}

	// 后台 特定条件财务信息
	public static function ProMenu($paraStr, $menuName='[此产品财务]', $color='#c9c9c9'){
		return '<div style="color:'. $color .';cursor:pointer;margin-top:3px;" onclick=\'OT_OpenMoneyRecord("'. $paraStr .'")\'>'. $menuName .'</div>';
	}

	// 会员中心菜单
	public static function UcMenu($mode='pc'){
		if ($mode == 'wap'){
			return '<li><a href="usersCenter.php?mudi=moneyRecord" class="active">财务明细</a></li>';
		}else{
			return '<li><a href="usersCenter.php?mudi=moneyRecord">财务明细</a></li>';
		}
	}

	// 会员中心信息统计栏
	public static function UcInfo($userID,$mode='pc'){
		global $DB;

		$whereTime = TimeDate::Add('d',-30,TimeDate::Get());
		$addNum = floatval($DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where MR_userID='. $userID .' and MR_money>=0 and MR_time>='. $DB->ForTime($whereTime)));
		$cutNum = floatval($DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where MR_userID='. $userID .' and MR_money<=0 and MR_time>='. $DB->ForTime($whereTime)));
		return '
			<div class="item">
				<div class="serNum">0'. (++ UsersCenter::$infoNum) .'</div>
				<div class="cont">
					财务
					<p class="number">30天内充值<span class="num1">'. OT::ToFloat($addNum) .'</span>元，支出<span class="num2">'. OT::ToFloat(abs($cutNum)) .'</span>元</p>
				</div>
				<div class="func"><a href="usersCenter.php?mudi=moneyRecord">管理</a></div>
			</div>
			<div class="clr"></div>
			';
	}

	// 财务记录列表
	public static function UcManage($userID,$mode='pc'){
		global $DB,$userRow,$userSysArr;

		$uexe = $DB->query('select UE_money,UE_payMoney from '. OT_dbPref .'users where UE_ID='. $userID);
		$urow = $uexe->fetch();
		$UE_money		= $urow['UE_money'];
		$UE_payMoney	= $urow['UE_payMoney'];

		$retStr = '
		<div style="padding:5px 0 10px 6px;">
			您的余额：<span style="color:red;font-weight:bold;">'. $UE_money .'</span> 元，共消费：<span style="color:blue;font-weight:bold;">'. $UE_payMoney .'</span>元
		</div>
		';

		if ($mode == 'uc'){
			$retStr .= '
			<div class="table-responsive">
				<table class="table table-hover">
				<thead>
				<tr>
					<th>编号</th>
					<th>发生时间</th>
					<th>金额</th>
					<th>余额</th>
					<th>备注</th>
				</tr>
				</thead>
				';
			$pageSize	= $userRow['UE_pageNum'];	// 每页条数
			$page		= OT::GetInt('page');
			$showRow=$DB->GetLimit('select MR_ID,MR_time,MR_dataName,MR_money,MR_remMoney,MR_note from '. OT_dbPref .'moneyRecord where MR_userID='. $userID .' and MR_userState=1 and MR_state=1 order by MR_ID DESC',$pageSize,$page);
			if (! $showRow){
				$retStr .= '</table><center class="font1_1 padd8">暂无记录</center>';
				return $retStr;
			}else{
				$recordCount=$DB->GetRowCount();
				$pageCount=ceil($recordCount/$pageSize);
				if ($page < 1 || $page > $pageCount){$page=1;}

				$retStr .= '<tbody class="tabBody">';
				$number=1+($page-1)*$pageSize;
				$rowCount = count($showRow);
				for ($i=0; $i<$rowCount; $i++){
					if ($i % 2 == 1){ $bgcolor='class="tabColorTr"'; }else{ $bgcolor=''; }
					$retStr .= '
					<tr id="data'. $showRow[$i]['MR_ID'] .'" '. $bgcolor .'>
						<td>'. $number .'</td>
						<td>'. $showRow[$i]['MR_time'] .'</td>
						<td style="padding-right:8px;">'. PayInfo::ColorNum($showRow[$i]['MR_money'],$showRow[$i]['MR_remMoney']) .'</td>
						<td style="padding-right:8px;">'. PayInfo::RemMoney($showRow[$i]['MR_remMoney']) .'</td>
						<td style="line-height:1.4;word-break:break-all;">'. $showRow[$i]['MR_note'] .'</td>
					</tr>
					';
					$number ++;
				}
				$retStr .= '
				</tbody>
				</table></div>

				'. UcBase::Nav($page, $pageCount, $pageSize, $recordCount) .'
				';
			}
			unset($showRow);

		}elseif ($mode == 'wap'){
			$retStr .= '
			<table cellpadding="0" cellspacing="0" border="0" class="tabList1">
			<thead>
			<tr>
				<td width="6%" align="center" style="text-align:center;">编号</td>
				<td width="24%" align="center" style="text-align:center;">发生时间<br />项目名称</td>
				<td width="16%" align="center" style="text-align:center;"><div style="text-align:right;">金额</div><div style="text-align:left;">余额</div></td>
				<td width="54%" align="center" style="text-align:center;">备注</td>
			</tr>
			</thead>
			';
			$pageSize	= $userRow['UE_pageWapNum'];	// 每页条数
			$page		= OT::GetInt('page');
			$showRow=$DB->GetLimit('select MR_ID,MR_time,MR_dataName,MR_money,MR_remMoney,MR_note from '. OT_dbPref .'moneyRecord where MR_userID='. $userID .' and MR_userState=1 and MR_state=1 order by MR_ID DESC',$pageSize,$page);
			if (! $showRow){
				$retStr .= '</table><div style="padding:8px;text-align:center;margin:0 auto;">暂无记录</div>';
				return $retStr;
			}else{
				$recordCount=$DB->GetRowCount();
				$pageCount=ceil($recordCount/$pageSize);
				if ($page < 1 || $page > $pageCount){$page=1;}

				$retStr .= '<tbody class="tabBody">';
				$number=1+($page-1)*$pageSize;
				$rowCount = count($showRow);
				for ($i=0; $i<$rowCount; $i++){
					if ($i % 2 == 1){ $bgcolor='class="tabColorTr"'; }else{ $bgcolor=''; }
					$showRow[$i]['MR_money']	= floatval($showRow[$i]['MR_money']);
					$showRow[$i]['MR_remMoney']	= floatval($showRow[$i]['MR_remMoney']);
					$retStr .= '
					<tr id="data'. $showRow[$i]['MR_ID'] .'" '. $bgcolor .'>
						<td align="center" style="text-align:center;">'. $number .'</td>
						<td align="center" style="text-align:center;">
							'. TimeDate::Get('m-d H:i',$showRow[$i]['MR_time']) .'
							<div style="text-align:center;padding-top:6px;">'. $showRow[$i]['MR_dataName'] .'</div>
						</td>
						<td align="right">
							<div style="text-align:right;padding-right:5px;">'. PayInfo::ColorNum($showRow[$i]['MR_money'],$showRow[$i]['MR_remMoney']) .'</div>
							<div style="padding-top:6px;">'. PayInfo::RemMoney($showRow[$i]['MR_remMoney']) .'</div>
						</td>
						<td align="left" style="line-height:1.4;word-break:break-all;">'. $showRow[$i]['MR_note'] .'</td>
					</tr>
					';
					$number ++;
				}
				$retStr .= '
				</tbody>
				</table>

				<center>'. WapList::Nav('page','',0,$page,$pageCount,$pageSize,$recordCount) .'</center>
				';
			}
			unset($showRow);

		}else{
			$retStr .= '
			<table cellpadding="0" cellspacing="0" border="0" class="tabList1">
			<thead>
			<tr>
				<td width="6%" align="center">编号</td>
				<td width="11%" align="center">发生时间</td>
				<td width="10%" align="center">金额</td>
				<td width="10%" align="center">余额</td>
				<td width="10%" align="center">机器编码</td>
				<td width="53%" align="center">备注</td>
			</tr>
			</thead>
			';
			$pageSize	= $userRow['UE_pageNum'];	// 每页条数
			$page		= OT::GetInt('page');
			$showRow=$DB->GetLimit('select MR_ID,MR_time,MR_dataName,MR_money,MR_remMoney,MR_note from '. OT_dbPref .'moneyRecord where MR_userID='. $userID .' and MR_userState=1 and MR_state=1 order by MR_ID DESC',$pageSize,$page);
			if (! $showRow){
				$retStr .= '</table><center class="font1_1 padd8">暂无记录</center>';
				return $retStr;
			}else{
				$recordCount=$DB->GetRowCount();
				$pageCount=ceil($recordCount/$pageSize);
				if ($page < 1 || $page > $pageCount){$page=1;}

				$retStr .= '<tbody class="tabBody">';
				$number=1+($page-1)*$pageSize;
				$rowCount = count($showRow);
				for ($i=0; $i<$rowCount; $i++){
					if ($i % 2 == 1){ $bgcolor='class="tabColorTr"'; }else{ $bgcolor=''; }
					$retStr .= '
					<tr id="data'. $showRow[$i]['MR_ID'] .'" '. $bgcolor .'>
						<td align="center">'. $number .'</td>
						<td align="center">'. $showRow[$i]['MR_time'] .'</td>
						<td align="right" style="padding-right:8px;">'. PayInfo::ColorNum($showRow[$i]['MR_money'],$showRow[$i]['MR_remMoney']) .'</td>
						<td align="right" style="padding-right:8px;">'. PayInfo::RemMoney($showRow[$i]['MR_remMoney']) .'</td>
						<td align="center">'. $showRow[$i]['MR_dataName'] .'</td>
						<td align="left" style="line-height:1.4;">'. $showRow[$i]['MR_note'] .'</td>
					</tr>
					';
					$number ++;
				}
				$retStr .= '
				</tbody>
				</table>

				<table cellpadding="0" cellspacing="0" align="center" class="listNavBox"><tr><td>
				'. Nav::Show('',$pageCount,$pageSize,$recordCount,'img','pageNum','get') .'
				</td></tr></table>
				';
			}
			unset($showRow);
		}

		return $retStr;
	}

	public static function IsAdd($money){
		if ($money!=0){ return true; }else{ return false; }
	}

	public static function AddData($sqlArr=array(), $mode=''){
		global $DB;

		$dataArr = array();
		$dataArr['MR_time']		= TimeDate::Get();
		$dataArr['MR_state']	= 1;

		if ($mode == 'get'){
			return $DB->Insert('moneyRecord',array_merge($dataArr,$sqlArr),$mode);
		}else{
			return $DB->InsertParam('moneyRecord',array_merge($dataArr,$sqlArr));
		}
	}
}
?>